//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.web.controller.member;

import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.Pagination;
import com.aidong.dao.wills.ContractReviewDao;
import com.aidong.dao.wills.ContractSignConfirmDao;
import com.aidong.dao.wills.MemberContractDao;
import com.aidong.dao.wills.MemberInfoDao;
import com.aidong.dao.wills.PtContractDao;
import com.aidong.model.wills.ContractInfo;
import com.aidong.model.wills.ContractReview;
import com.aidong.model.wills.ContractSignConfirm;
import com.aidong.model.wills.MemberContract;
import com.aidong.model.wills.MemberInfo;
import com.aidong.model.wills.PtContract;
import com.aidong.util.ExportXls;
import com.aidong.web.controller.BaseController;
import com.aidong.web.controller.svc.ContractSignController;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"contractReview"})
public class ContractReviewController extends BaseController {
    @Resource
    ContractReviewDao contractReviewDao;
    @Resource
    MemberContractDao memberContractDao;
    @Resource
    PtContractDao ptContractDao;
    @Resource
    MemberInfoDao memberInfoDao;
    @Resource
    ContractSignConfirmDao contractSignConfirmDao;

    public ContractReviewController() {
    }

    @RequestMapping({"list"})
    public ModelAndView list() {
        ModelAndView mv = new ModelAndView("member/review/list");
        mv.addObject("reviewStatus", this.getTypeList("reviewStatus"));
        return mv;
    }

    @RequestMapping({"_list"})
    public ModelAndView _list(@RequestParam(value = "shopCode",required = false) String shopCode, @RequestParam(value = "status",required = false) Integer status, @RequestParam(value = "type",required = false) String type, @RequestParam(value = "contractCode",required = false) String contractCode, @RequestParam(value = "dateType",required = false) String dateType, @RequestParam(value = "dateFrom",required = false) String dateFrom, @RequestParam(value = "dateTo",required = false) String dateTo, @RequestParam(value = "pageNo",required = false) String pageNo, @RequestParam(value = "totalCount",required = false) String totalCount) {
        Pagination pg = new Pagination(1, 20);
        pg.setPage(this.parseInt(pageNo), this.parseInt(totalCount));
        ConditionMap cd = new ConditionMap();
        this.addConditionShopCode(cd, "shopCode", shopCode);
        cd.eq("contractCode", contractCode);
        if(!this.chkNull(status)) {
            cd.eq("reviewStatus", status);
        } else {
            cd.gt("reviewStatus", Integer.valueOf(0));
        }

        if("2".equals(dateType)) {
            if(!this.chkNull(dateFrom)) {
                cd.ge("reviewDate", this.parseDate(dateFrom, "yyyy-MM-dd"));
            }

            if(!this.chkNull(dateTo)) {
                cd.le("reviewDate", this.parseDate(dateTo + " 23:59:59", "yyyy-MM-dd HH:mm:ss"));
            }
        } else {
            if(!this.chkNull(dateFrom)) {
                cd.ge("saleDate", this.parseDate(dateFrom, "yyyy-MM-dd"));
            }

            if(!this.chkNull(dateTo)) {
                cd.le("saleDate", this.parseDate(dateTo + " 23:59:59", "yyyy-MM-dd HH:mm:ss"));
            }
        }

        cd.eq("signStatus", Integer.valueOf(1));
        this.contractReviewDao.queryPageList(pg, cd, type);
        ModelAndView mv = new ModelAndView("member/review/_list");
        mv.addObject("list", pg.getResultList());
        mv.addObject("pg", pg);
        return mv;
    }

    @RequestMapping({"expt"})
    public ModelAndView expt(@RequestParam(value = "shopCode",required = false) String shopCode, @RequestParam(value = "status",required = false) Integer status, @RequestParam(value = "type",required = false) String type, @RequestParam(value = "contractCode",required = false) String contractCode, @RequestParam(value = "dateType",required = false) String dateType, @RequestParam(value = "dateFrom",required = false) String dateFrom, @RequestParam(value = "dateTo",required = false) String dateTo, HttpServletResponse response) {
        try {
            Pagination e = new Pagination(1, '\uea60');
            e.setPage(Integer.valueOf(1), Integer.valueOf(1));
            ConditionMap cd = new ConditionMap();
            this.addConditionShopCode(cd, "shopCode", shopCode);
            cd.eq("contractCode", contractCode);
            if(!this.chkNull(status)) {
                cd.eq("reviewStatus", status);
            } else {
                cd.gt("reviewStatus", Integer.valueOf(0));
            }

            if("2".equals(dateType)) {
                if(!this.chkNull(dateFrom)) {
                    cd.ge("reviewDate", this.parseDate(dateFrom, "yyyy-MM-dd"));
                }

                if(!this.chkNull(dateTo)) {
                    cd.le("reviewDate", this.parseDate(dateTo + " 23:59:59", "yyyy-MM-dd HH:mm:ss"));
                }
            } else {
                if(!this.chkNull(dateFrom)) {
                    cd.ge("saleDate", this.parseDate(dateFrom, "yyyy-MM-dd"));
                }

                if(!this.chkNull(dateTo)) {
                    cd.le("saleDate", this.parseDate(dateTo + " 23:59:59", "yyyy-MM-dd HH:mm:ss"));
                }
            }

            cd.eq("signStatus", Integer.valueOf(1));
            this.contractReviewDao.queryPageList(e, cd, type);
            ExportXls xls = new ExportXls(response, "合同内审", "合同内审");
            xls.addHead(new String[]{"门店编号", "门店名称", "销售日期", "类型", "合同号", "会员号", "会员姓名", "卡种/课程", "合同金额", "定金金额", "付款金额", "销售员", "销售员2", "短信验证", "验证手机", "内审状态", "内审时间", "提交时间"});
            xls.setWidth(new int[]{10, 10, 20, 10, 15, 18, 10, 20, 10, 10, 10, 15, 15, 10, 15, 10, 20, 20});
            new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Iterator i$ = e.getResultList().iterator();

            while(i$.hasNext()) {
                ContractInfo contract = (ContractInfo)i$.next();
                xls.addRow(new Object[]{contract.getShopCode(), this.cache.getValue("shopName", contract.getShopCode()), sdf2.format(contract.getSaleDate()), "1".equals(contract.getType())?"会籍":("2".equals(contract.getType())?"私教":""), contract.getContractCode(), contract.getMemberCode(), contract.getMemberName(), "1".equals(contract.getType())?this.cache.getValue("cardType", contract.getCardType()):("2".equals(contract.getType())?this.cache.getValue("ptItemName", contract.getPtCode()):""), contract.getFactPrice(), contract.getEarnestAmount(), contract.getPayAmount(), this.nvl(contract.getSalesCode()) + " - " + this.nvl(this.cache.getValue("salesName", contract.getSalesCode())), this.nvl(contract.getSalesCode2()) + " - " + this.nvl(this.cache.getValue("salesName", contract.getSalesCode2())), this.chkNullZ(contract.getVerifyStatus())?null:this.cache.getValue("verifyStatus", String.valueOf(contract.getVerifyStatus())), contract.getVerifyMobile(), this.cache.getValue("reviewStatus", String.valueOf(contract.getReviewStatus())), contract.getReviewDate() != null?sdf2.format(contract.getReviewDate()):"", contract.getReviewSubmitDate() != null?sdf2.format(contract.getReviewSubmitDate()):""});
            }

            xls.close();
            this.addUserLog("导出合同内审", e.getResultList().size() + "条记录");
        } catch (Exception var16) {
            this.log.error("import", var16);
        }

        return null;
    }

    @RequestMapping({"view"})
    public ModelAndView view(@RequestParam("tp") String tp, @RequestParam("id") int id) {
        return this.review(tp, id, "view");
    }

    @RequestMapping({"review1"})
    public ModelAndView review1(@RequestParam("tp") String tp, @RequestParam("id") int id) {
        return this.review(tp, id, "review");
    }

    @RequestMapping({"review2"})
    public ModelAndView review2(@RequestParam("tp") String tp, @RequestParam("id") int id) {
        return this.review(tp, id, "review");
    }

    private ModelAndView review(String tp, int id, String view) {
        Object contract = null;
        String memberCode = null;
        if("1".equals(tp)) {
            contract = this.memberContractDao.getById(Integer.valueOf(id));
            if(contract != null) {
                memberCode = ((MemberContract)contract).getMemberCode();
            }
        } else if("2".equals(tp)) {
            contract = this.ptContractDao.getById(Integer.valueOf(id));
            if(contract != null) {
                memberCode = ((PtContract)contract).getMemberCode();
            }
        }

        if(contract == null) {
            return this.showAlert("没有这个合同");
        } else {
            MemberInfo member = this.memberInfoDao.getByMemberCode(memberCode);
            if(member == null) {
                return this.showAlert("会员信息错误");
            } else {
                List reviewList = this.contractReviewDao.getListBySaleId(tp, id);
                ContractSignConfirm contractSign = this.contractSignConfirmDao.getBySaleId(id, "0" + tp);
                if(contractSign != null) {
                    contractSign.setAuthCode(ContractSignController.getAuthCode(new String[]{contractSign.getContractCode(), contractSign.getContractType()}));
                }

                ModelAndView mv = new ModelAndView("member/review/" + view);
                mv.addObject("type", tp);
                mv.addObject("contract", contract);
                mv.addObject("member", member);
                mv.addObject("reviewList", reviewList);
                mv.addObject("sign", contractSign);
                mv.addObject("_picPath", this.getSetting("pic.path"));
                mv.addObject("_oper", view);
                return mv;
            }
        }
    }

    @RequestMapping({"_review1"})
    @ResponseBody
    public String _review1(@ModelAttribute("review") ContractReview review) {
        return this._review(review, 1);
    }

    @RequestMapping({"_review2"})
    @ResponseBody
    public String _review2(@ModelAttribute("review") ContractReview review) {
        return this._review(review, 2);
    }

    private String _review(ContractReview review, int st) {
        if(!this.chkNull(review.getSaleType()) && !this.chkNullZ(review.getSaleId())) {
            String memo = "";
            String nam = st == 3?"重新提交":"审核";
            if("1".equals(review.getSaleType())) {
                MemberContract e = (MemberContract)this.memberContractDao.getById(review.getSaleId());
                if(e == null) {
                    return this.getResult(false, "没有这个合同");
                }

                if(e.getReviewStatus().intValue() != st) {
                    return this.getResult(false, "不能" + nam);
                }

                memo = "合同号:" + e.getContractCode() + ",类型:会籍";
            } else if("2".equals(review.getSaleType())) {
                PtContract e1 = (PtContract)this.ptContractDao.getById(review.getSaleId());
                if(e1 == null) {
                    return this.getResult(false, "没有这个合同");
                }

                if(e1.getReviewStatus().intValue() != st) {
                    return this.getResult(false, "不能" + nam);
                }

                memo = "合同号:" + e1.getContractCode() + ",类型:私教";
            }

            review.setReviewType(Integer.valueOf(st));
            review.setReviewUser(this.getLoginUserCode());
            review.setReviewTime(new Date());

            try {
                this.contractReviewDao.saveReview(review);
                this.addUserLog("内审" + nam, memo);
            } catch (Exception var6) {
                this.log.error("review contract", var6);
                return this.getResult(false, nam + "发生错误");
            }

            return this.getResult(true, nam + "完成");
        } else {
            return this.getResult(false, "没有这个合同");
        }
    }

    @RequestMapping({"_resubmit"})
    @ResponseBody
    public String _resubmit(@ModelAttribute("review") ContractReview review) {
        review.setReviewResult(Integer.valueOf(1));
        return this._review(review, 3);
    }

    @RequestMapping({"_chgverify"})
    @ResponseBody
    public String _chgverify(@RequestParam("tp") String tp, @RequestParam("id") int id) {
        MemberContract contract = (MemberContract)this.memberContractDao.getById(Integer.valueOf(id));
        if(contract == null) {
            return this.getResult(false, "没有这个合同");
        } else if(this.chkNullZ(contract.getVerifyStatus()) || contract.getVerifyStatus().intValue() != 1 && contract.getVerifyStatus().intValue() != 3) {
            this.memberContractDao.updateVerifyStatus(id, 3);
            this.addUserLog("内审通过短信验证", "合同号:" + contract.getContractCode());
            return this.getResult(true, "操作成功");
        } else {
            return this.getResult(false, "已经验证");
        }
    }
}
